package com.hexiang.web.response;

import com.hexiang.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResponse<T extends Serializable> implements Serializable {


    private T data;
    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 返回码
     */
    private String code;
    /**
     * 返回描述信息
     */
    private String message;

    public static CommonResponse fail(String code, String message) {
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.setCode(code);
        commonResponse.setMessage(message);
        return commonResponse;
    }

    public static CommonResponse fail(String message) {
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.setCode(ResultCodeEnum.S200.getCode());
        commonResponse.setMessage(ResultCodeEnum.S200.getEmsg());
        return commonResponse;
    }

    public static <T extends Serializable> CommonResponse<T> success(T data) {
        CommonResponse<T> response = new CommonResponse<T>();
        response.setCode(ResultCodeEnum.S200.getCode());
        response.setMessage(ResultCodeEnum.S200.getEmsg());
        response.setData(data);
        return response;
    }

    public static <T extends Serializable> CommonResponse<T> success() {
        CommonResponse<T> response = new CommonResponse<T>();
        response.setCode(ResultCodeEnum.S200.getCode());
        response.setMessage(ResultCodeEnum.S200.getEmsg());
        return response;
    }

    public boolean isSuccess() {
        return ResultCodeEnum.S200.getCode().equals(this.getCode());
    }
}